/*******************************************************************************
*   			Impacts of a Large-Scale Parenting Program: 				   *
*					Experimental Evidence from Chile						   *
********************************************************************************


	REQUIRES:	"${dt_final}panel_RR_indexes.dta"
	CREATES:	${out_tables}/T33A_facilitator_bytreatment.xls
				
	WRITEN BY:  Italo Lopez  [italolop@usc.edu]

********************************************************************************
	Prepare data
*******************************************************************************/

	* Load data
	use "${dt_final}/panel_RR_indexes.dta", clear


	* Establish macros for regressions

/*******************************************************************************
*   Set macros for regressions
*******************************************************************************/
	
	* Outcomes
	*-----------------------------------------------------------
	local childoutcomes  "zDCCSPunt zptevir_irtscore2 zt_score zcbcl_t_e zcbcl_t_i zibatt_sspi zibatt_ssai zibatt_sssr zas_raw_score" 
	local behaviors "home_score se_score disc_score" 
	local beliefs "zpstyle1 zpstyle2 zpstyle3 zPSCS zPSSS_family zPSSS_friends zPSSS_others zPSI_Distress zCESD zpacotis"	
	local child "zptevir_irtscore2 zcog_index zsoc_index" 

	* Controls
	*-----------------------------------------------------------

	macro  def Xvar1 "i.age_year i.gender"  	
	macro  def Xvar2 "i.age_year i.gender inc_qtaut_old i.pc_edu_mdsfin hh_mem hh_tipo"  	
	macro  def Xvar2old "i.gender inc_qtaut_old i.pc_edu_mdsfin_old hh_mem hh_tipo"  

	
	* Parental characteristics: WAIS and BFI and imputed wais 
	*-----------------------------------------------------------

	macro def parent_cha4 "BFI_ext BFI_ope BFI_con BFI_neu BFI_agr wais2 flag_wais"
	

/*******************************************************************************
Table A34: Heterogeneity of impact: Facilitator Background
*******************************************************************************/
	* Set seed
	
	set seed 1234

	*-------------------------------------
	* Create multilevel facilitator background
	*-------------------------------------


	gen fac_profmiss=0 if fac_profesion!=. & FOLIO_UC!=.
    replace fac_profmiss=0 if fac_profesion==. & FOLIO_UC!=.& TIPO==1
    replace fac_profmiss=1 if fac_profesion==. & FOLIO_UC!=.& TIPO!=1
		
	bys CENTRO_SALUD TIPO: gen centrotipo=_n	

	
	
	
	*-------------------------------------
	* Frequency
	*-------------------------------------
	

	tab fac_profesion TIPO if centrotipo==1, col matcell(M)

	local b =  M[1,1] + M[2,1] + M[3,1] + M[4,1]
	local nurse_nepb = string(M[1,1]*100/`b',"%9.1f")
	local psyc_nepb = string(M[2,1]*100/`b',"%9.1f")
	local educ_nepb = string(M[3,1]*100/`b',"%9.1f")
	local socw_nepb = string(M[4,1]*100/`b',"%9.1f")
	local tot_nepb = string((M[1,1] + M[2,1])*100 / `b',"%9.1f")

	local i = M[1,2] + M[2,2] + M[3,2] + M[4,2]
	local nurse_nepi = string(M[1,2]*100/`i',"%9.1f")
	local psyc_nepi = string(M[2,2]*100/`i',"%9.1f")
	local educ_nepi = string(M[3,2]*100/`i',"%9.1f")
	local socw_nepi = string(M[4,2]*100/`i',"%9.1f")
	local tot_nepi = string((M[1,2] + M[2,2])*100 / `i',"%9.1f")
			
	local nurse_tot = string((M[1,1] +M[1,2])*100/( `b' +   `i'),"%9.1f")
	local psyc_tot = string((M[2,1] +M[2,2])*100/( `b' + `i'),"%9.1f")
	local educ_tot = string((M[3,1] +M[3,2])*100/( `b' + `i'),"%9.1f")
	local socw_tot = string((M[4,1] +M[4,2])*100/( `b' + `i'),"%9.1f")
	local tot_tot = string(((M[1,1] +M[1,2]) + ( M[2,1] +M[2,2]) )*100 / (`b' + `i'),"%9.1f")
			
			
	putexcel set "${out_tables}/appendix/T33A_facilitator_bytreatment.xls", modify 
	putexcel A1  =  "Table A33: Facilitator Background by Treatment Arm"
	putexcel A2  =  "TIPO"

	putexcel B2  =  "NEP-B"
	putexcel C2  =  "NEP-I"
	putexcel D2  =  "Total"
	putexcel A3  =  "1. Nurses"
	putexcel A4  =  "2. Psychologists"
	putexcel A5  =  "3. Educators"
	putexcel A6  =  "4. Social workers"
	putexcel A7  =  "Health Workers (1+2) "
	
	putexcel B3  =  "`nurse_nepb'%"
	putexcel B4  =  "`psyc_nepb'%"
	putexcel B5  =  "`educ_nepb'%"
	putexcel B6  =  "`socw_nepb'%"
	putexcel B7  =  "`tot_nepb'%"
	
	putexcel C3  =  "`nurse_nepi'%"
	putexcel C4  =  "`psyc_nepi'%"
	putexcel C5  =  "`educ_nepi'%"
	putexcel C6  =  "`socw_nepi'%"
	putexcel C7  =  "`tot_nepi'%"
	
	putexcel D3  =  "`nurse_tot'%"
	putexcel D4  =  "`psyc_tot'%"
	putexcel D5  =  "`educ_tot'%"
	putexcel D6  =  "`socw_tot'%"
	putexcel D7  =  "`tot_tot'%"
	
	